package com.highcom.service.impl;

import com.highcom.constant.MessageConstant;
import com.highcom.entity.Result;
import com.highcom.mapper.MemberDao;
import com.highcom.mapper.OrderDao;
import com.highcom.mapper.TOrdersettingMapper;
import com.highcom.model.Member;
import com.highcom.model.Order;
import com.highcom.model.TOrdersetting;
import com.highcom.model.TOrdersettingExample;
import com.highcom.service.OrderService;
import com.highcom.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/8/7 9:56
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Resource
    private TOrdersettingMapper tOrdersettingMapper;
    @Resource
    private MemberDao memberDao;
    @Resource
    private OrderDao orderDao;
    /*
    order方法业务逻辑分析：
        1.根据所选日期查询预约表，拿到可预约数量与已预约数量
        2.进行可预约与已预约的大小分析，确定是否可以继续预约
        3.获取预约人的手机号码，查询是否是本系统会员，如不是给予注册，如是在已经预约表里查询是否有重复预约
        4.上述条件都满足后，可成功预约，并且在已预约——待预约表内将当天已预约人数+1，返回预约成功
    */
    @Override
    public Result order(Map map) throws Exception {
        Date date = DateUtils.parseString2Date((String) map.get("orderDate"));
        TOrdersettingExample tOrdersettingExample = new TOrdersettingExample();
        tOrdersettingExample.createCriteria().andOrderdateEqualTo(date);
        List<TOrdersetting> tOrdersettings = tOrdersettingMapper.selectByExample(tOrdersettingExample);

        TOrdersetting tOrdersetting = null;
        if(tOrdersettings!=null&&tOrdersettings.size()>0){
            tOrdersetting = tOrdersettings.get(0);
        } else {
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        Integer number = tOrdersetting.getNumber();
        Integer reservations = tOrdersetting.getReservations();
        if(number<=reservations){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if(member != null){
            Integer id = member.getId();
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(id,date,Integer.parseInt(setmealId));
            List<Order> list = orderDao.findByCondition(order);
            if (list != null & list.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        } else {
            member = new Member();
            member.setName((String)map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String)map.get("idCard"));
            member.setSex((String)map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType((String)map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));//套餐id
        orderDao.add(order);

        tOrdersetting.setReservations(tOrdersetting.getReservations()+1);
        tOrdersettingMapper.updateByPrimaryKeySelective(tOrdersetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            Date orderDate = (Date)map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
